#!/usr/bin/python3.7.3
# !coding:utf-8
import utils


class Extension():

    def __init__(self):
        self.api = 'app/conference_centers/'
        self.api_list = self.api + 'conference_rooms.php'
        self.api_edit = self.api + 'conference_room_edit.php'
        self.url_edit_param = ""
        self.url_get_param = "?search={0}&submit=Search"
        pass

    def add(self, extension='', display_name=''):
        edit_html = utils.get(self.api_edit).content
        bs4 = utils.spider(edit_html)
        # domain_uuid = bs4.find("select", attrs={"name": "domain_uuid"})
        # domain_uuid = bs4.select_one("").select_one("")
        domain_uuid = bs4.select_one("select[name='domain_uuid'] > option[selected='selected']").get("value")
        domain = bs4.select_one("input[name='user_context']").get("value")

        url = self.url_edit_param.format(None, extension, display_name, domain, domain_uuid)
        params = utils.url2json(url)
        resp = utils.post(self.api_edit, params)
        if (resp.status_code == 302):
            import re
            location = resp.headers['Location']
            match = re.search('id=(.+)$', location)
            if (match != None):
                return match.group(1)

        return None

    def edit(self, extension_uuid='', params={}):
        pass
        # url = self.url_edit_param.format(extension_uuid, extension, display_name, domain, domain_uuid)
        # params = utils.url2json(url)
        # resp = utils.post(self.api_edit, params)

    def get(self, extension='', ):
        if (extension == ''):
            return None
        return self.list(extension, list_one=True)

    def list(self, extension='', list_one=False):
        url = self.api_list + self.url_get_param.format(extension)
        resp = utils.get(url)
        bs4 = utils.spider(resp.content)
        list = bs4.select("#main_content form tr td.tr_link_void input[type='checkbox']")

        res = []
        for input in list:
            id = input.get("value")
            if (id != "" and id != None):
                resp2 = utils.get(self.api_edit + "?id=" + id)
                form = utils.spider(resp2.content).select("#main_content form input,select")
                obj = {}
                for tag in form:
                    tagName = tag.name
                    key = tag.get("name")
                    value = ""
                    if (key == "" or key == None):
                        continue
                    if ("input" == tagName):
                        # inputType = tag.get("type")
                        value = tag.get("value")
                        pass
                    if ("select" == tagName):
                        chk = tag.select_one("option[checked='checked']")
                        value = (None if (chk == None) else chk.get("value"))

                    if (value != "" and value != None):
                        obj[key] = value
                res.append(obj)

        if (len(res) > 0):
            return (res if (list_one == False) else res[0])

        return res


if __name__ == '__main__':
    ext = Extension()
    # id = ext.add('1234', '钱多多')
    # print(id)

    # res = ext.list('100')
    # print(res)
    #
    # res = ext.get('1001')
    # print(res)
